package com.bjsj.pro.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.bjsj.pro.R;
import com.bjsj.pro.tools.Configs;
import com.bjsj.pro.ui.BannderDetailsActivity;
import com.bjsj.pro.ui.primaryActivity;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 隐私协议
 */
public class PrimaryDialog extends Dialog implements View.OnClickListener, AdapterView.OnItemClickListener {

    // @BindView(R.id.tv_userHelp)
    TextView tvUserHelp;
  //  @BindView(R.id.tv_primary)
    TextView tvPrimary;
  //  @BindView(R.id.tv_confirm)
    TextView tvConfirm;
 //   @BindView(R.id.tv_cancel)
    TextView tvCancel;
 //   @BindView(R.id.checkPrimary)
    CheckBox checkPrimary;
    private Activity context;
    private PrimaryDialogListener mListener;

    public PrimaryDialog(@NonNull Activity context,PrimaryDialogListener mListener) {
        super(context);
        this.context = context;
        this.mListener=mListener;
        setCanceledOnTouchOutside(false);

    }

    public interface PrimaryDialogListener
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id);
    }

    /**
     * 取消事件监听接口
     */
    private PrimaryDialog.PrimaryDialogCancelListener mCancelListener;

    public interface PrimaryDialogCancelListener
    {
        public void onCancelClick(View v);
    }

   /* @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_primary;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        ButterKnife.bind(this);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        ButterKnife.bind(this).unbind();
    }*/


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        View view = getLayoutInflater().inflate(R.layout.dialog_primary,
                null);
        setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        Window window = getWindow();
        // 设置显示动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y =0;//context.getWindowManager().getDefaultDisplay().getHeight();
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width =800; //ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        // 设置显示位置
        onWindowAttributesChanged(wl);
        initview();
    }

    public void initview(){
        tvUserHelp=(TextView)findViewById(R.id.tv_userHelp);
        tvPrimary=(TextView)findViewById(R.id.tv_primary);
        tvConfirm=(TextView)findViewById(R.id.tv_confirm);
        tvCancel=(TextView)findViewById(R.id.tv_cancel);
        checkPrimary=(CheckBox)findViewById(R.id.checkPrimary);
        tvPrimary.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2 = new Intent(context, primaryActivity.class);
                context.startActivity(intent2);
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkPrimary.isChecked()){
                    saveObject(context, Configs.SAVE_ISYS_KEY, Configs.SAVE_ISYS_NAME,"1");
                    dismiss();
                }else{
                    Toast.makeText(context,"(请先勾选隐私协议后同意)",Toast.LENGTH_SHORT).show();
                }
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });
    }


  //  @OnClick({R.id.tv_primary, R.id.tv_confirm, R.id.tv_cancel})
    public void onClick(View v) {
      /*  switch (v.getId()) {
            case R.id.tv_primary:
                Intent intent2 = new Intent(context, BannderDetailsActivity.class);
                context.startActivity(intent2);
                break;
            case R.id.tv_confirm:
                if(checkPrimary.isChecked()){
                   saveObject(context, Configs.SAVE_ISYS_KEY, Configs.SAVE_ISYS_NAME,"1");
                    dismiss();
                }else{
                    Toast.makeText(context,"(请先勾选隐私协议后同意)",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_cancel:

                break;
        }*/
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mListener.onItemClick(parent, view, position, id);
        dismiss();
    }

    public void saveObject(Context context, String key, String spName, Object obj) {
        try {
            // 保存对象
            SharedPreferences.Editor sharedata = context.getSharedPreferences(spName, 0).edit();
            //先将序列化结果写到byte缓存中，其实就分配一个内存空间
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(bos);
            //将对象序列化写入byte缓存
            os.writeObject(obj);
            //将序列化的数据转为16进制保存
            String bytesToHexString = bytesToHexString(bos.toByteArray());
            //保存该16进制数组
            sharedata.putString(key, bytesToHexString);
            sharedata.commit();
            Log.e("==================", "保存obj成功");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("==================", "保存obj失败");
        }
    }

    /**
     * desc:将数组转为16进制
     *
     * @param bArray
     * @return modified:
     */
    public String bytesToHexString(byte[] bArray) {
        if (bArray == null) {
            return null;
        }
        if (bArray.length == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
}
